package com.baizhi.service.impl;

import com.baizhi.common.Constant;
import com.baizhi.dao.StudentDao;
import com.baizhi.entity.Student;
import com.baizhi.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;
    @Autowired
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public void insert(Student s) {
        studentDao.insert(s);
    }

    @Override
    public void delete(Integer id) {
        //删除一个对象之前  先将此对象的头像文件删除
        Student student = studentDao.selectById(id);
        //获取该对象的头像在虚拟目录中保存的文件名
        String filename = student.getNewfilename();
        //利用文件File的delete方法将其从磁盘删除
        File file = new File(Constant.IMG_LOCATION,filename);
        file.delete();
        //然后再将这个对象删除
        studentDao.delete(id);
    }

    @Override
    public void deleteAny(Integer[] ids) {
        for (Integer id : ids) {
            //删除一个对象之前  先将此对象的头像文件删除
            Student student = studentDao.selectById(id);
            //获取该对象的头像在虚拟目录中保存的文件名
            String filename = student.getNewfilename();
            //利用文件File的delete方法将其从磁盘删除
            File file = new File(Constant.IMG_LOCATION,filename);
            file.delete();
            //然后再将这个对象删除
            studentDao.delete(id);
        }
//        studentDao.deleteAny(ids);
    }

    @Override
    public Student selectById(Integer id) {
        Student student = studentDao.selectById(id);
        return student;
    }

    @Override
    public void update(Student s) {
        studentDao.update(s);
    }

    @Override
    public PageInfo<Student> selectStudents(String sname,Integer pageNum, Integer pageSize) {
        System.out.println("业务层参数sname="+sname+",pageNum="+pageNum);
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = studentDao.selectStudents(sname);
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        for (Student s : students) {
            System.out.println(s);
        }
        return pageInfo;
    }
}
